package producers;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.JsonObject;

import net.sf.json.JSONObject;
import producers.SIPAccount.Action;
import utils.WSUtil;

/**
 * 技能组
 * @author jinchaoyang
 */
public class SkillProducer implements ISkillProducer {
    private final static String BASE_URL="https://api.ucpaas.com/2014-06-30";

    
    public String sid;
    
    public String token;
    
    public String appId;
    
    public SkillProducer(String sid,String token,String appId){
        this.sid = sid;
        this.token = token;
        this.appId = appId;
    }
    
    
    public  Map<String,String> create(String sid,String token,String appId, String queueId,Map<String,Object> options){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("appId", appId);
        params.put("queueId", queueId);
        params.put("maxQNum",0);
        params.put("key2ExitQ", "#");
        params.put("waitTimeLen", 20);
        params.put("key2Continue", "*");
        params.put("maxQCnt", 3);
        if(null!=options){
            params.putAll(options);
        }
        JSONObject obj = new JSONObject();
        obj.put("ivr", JSONObject.fromObject(params));
        JsonObject rs = WSUtil.httpsPost(BASE_URL+Action.CREATE.getUrl(),sid, token, obj.toString());
        Map<String,String> result = parseResult(rs);
        result.put("appId", appId);
        result.put("queueId",queueId);
        return result;
    }
    
    public Map<String,String> update(String sid,String token,String appId, String queueId,Map<String,Object> options){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("appId", appId);
        params.put("queueId", queueId);
        params.put("maxQNum",0);
        params.put("key2ExitQ", "#");
        params.put("waitTimeLen", 20);
        params.put("key2Continue", "*");
        params.put("maxQCnt", 3);
        if(null!=options){
            params.putAll(options);
        }
        JSONObject obj = new JSONObject();
        obj.put("ivr", JSONObject.fromObject(params));
        JsonObject rs = WSUtil.httpsPost(BASE_URL+Action.UPDATE.getUrl(),sid, token, obj.toString());
        Map<String,String> result = parseResult(rs);
        result.put("appId", appId);
        result.put("queueId",queueId);
        return result;
    }
    
    public Map<String,String> destroy(String sid,String token,String appId,String queueId){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("appId", appId);
        params.put("queueId", queueId);
        JSONObject obj = new JSONObject();
        obj.put("ivr", JSONObject.fromObject(params));
        JsonObject rs = WSUtil.httpsPost(BASE_URL+Action.DESTROY.getUrl(),sid, token, obj.toString());
        Map<String,String>  result = parseResult(rs);
        result.put("appId", appId);
        result.put("queueId",queueId);
        return result;
    }
    
    
    private Map<String,String> parseResult(JsonObject obj){
        Map<String,String> result = new HashMap<String,String>();
        String rcode = result.get("respCode");
        result.put("rcode", rcode);
        return result;
    }
    
    
   enum Action{
       CREATE("/Accounts/{sid}/ipcc/queue/create"),
       UPDATE("/Accounts/{sid}/ipcc/queue/edit"),
       DESTROY("/Accounts/{sid}/ipcc/queue/del");
       
       private String url;
       
       private Action(String url){
          this.url = url;
       }
       
       public String getUrl(){
           return this.url;
       }
   }
   
    
    
}
